//
//  LoginViewController.m
//  ChatForHot
//
//  Created by FLY on 2018/10/10.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isHiddenNav = YES;
    
    
}
- (void)HOTCHATInsertSubViews{
    
    [self.view addSubview:self.HOTCHATbackImgView];
    [self.view addSubview:self.HOTCHATbackBtn];
    [self.view addSubview:self.HOTCHATlogoImgView];
    [self.view addSubview:self.HOTCHATaccountView];
    [self.view addSubview:self.HOTCHATaccountlogoImgView];
    [self.view addSubview:self.HOTCHATaccounttextField];
    [self.view addSubview:self.HOTCHATpassWordView];
    [self.view addSubview:self.HOTCHATpassWordlogoImgView];
    [self.view addSubview:self.HOTCHATpassWordtextField];
    [self.view addSubview:self.HOTCHATloginBtn];
    
    
    [self.HOTCHATbackImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
    [self.HOTCHATbackBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).mas_offset(34);
        make.left.equalTo(self.view.mas_left).mas_offset(15);
    }];
    [self.HOTCHATlogoImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).mas_offset(140);
        make.centerX.equalTo(self.view);
    }];
    
    [self.HOTCHATaccountView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.HOTCHATlogoImgView.mas_bottom).mas_offset(90);
        make.left.equalTo(self.view.mas_left).mas_offset(30);
        make.right.equalTo(self.view.mas_right).mas_offset(-30);
        make.height.mas_equalTo(44);
    }];
    [self.HOTCHATaccountlogoImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.HOTCHATaccountView.mas_centerY);
        make.left.equalTo(self.HOTCHATaccountView.mas_left).mas_offset(20);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(17);
    }];
    [self.HOTCHATaccounttextField mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.HOTCHATaccountView.mas_centerY);
        make.left.equalTo(self.HOTCHATaccountlogoImgView.mas_right).mas_offset(10);
        make.right.equalTo(self.HOTCHATaccountView.mas_right).mas_offset(-10);
        
    }];
    
    [self.HOTCHATpassWordView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.HOTCHATaccountView.mas_bottom).mas_offset(22);
        make.left.equalTo(self.view.mas_left).mas_offset(30);
        make.right.equalTo(self.view.mas_right).mas_offset(-30);
        make.height.mas_equalTo(44);
    }];
    [self.HOTCHATpassWordlogoImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.HOTCHATpassWordView.mas_centerY);
        make.left.equalTo(self.HOTCHATpassWordView.mas_left).mas_offset(20);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(17);
    }];
    [self.HOTCHATpassWordtextField mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.HOTCHATpassWordView.mas_centerY);
        make.left.equalTo(self.HOTCHATpassWordlogoImgView.mas_right).mas_offset(10);
        make.right.equalTo(self.HOTCHATpassWordView.mas_right).mas_offset(-10);
        
    }];
    
    [self.HOTCHATloginBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.view.mas_left).mas_offset(30);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-30);
        make.top.mas_equalTo(self.HOTCHATpassWordView.mas_bottom).mas_offset(56);
    }];
}
//lazzy
- (UIImageView *)HOTCHATbackImgView{
    
    if (_HOTCHATbackImgView == nil) {
        
        _HOTCHATbackImgView = [[UIImageView alloc]init];
        _HOTCHATbackImgView.image = [UIImage imageNamed:@"l_bg"];
        _HOTCHATbackImgView.userInteractionEnabled = YES;
    }
    return _HOTCHATbackImgView;
}
- (UIButton *)HOTCHATbackBtn{
    
    if (_HOTCHATbackBtn == nil) {
        
        _HOTCHATbackBtn = [[UIButton alloc]init];
        [_HOTCHATbackBtn setBackgroundImage:[UIImage imageNamed:@"l_back"] forState:UIControlStateNormal];
        [_HOTCHATbackBtn setBackgroundImage:[UIImage imageNamed:@"l_back_hig"] forState:UIControlStateHighlighted];
        [_HOTCHATbackBtn addTarget:self action:@selector(HOTCHATbackAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _HOTCHATbackBtn;
}
-(UIImageView *)HOTCHATlogoImgView{
    
    if (_HOTCHATlogoImgView == nil) {
        
        _HOTCHATlogoImgView = [[UIImageView alloc]init];
        _HOTCHATlogoImgView.image = [UIImage imageNamed:@"l_bg_top_logo"];
    }
    return _HOTCHATlogoImgView;
}
- (UIView *)HOTCHATaccountView{
    
    if (_HOTCHATaccountView == nil) {
        
        _HOTCHATaccountView = [[UIView alloc]init];
        _HOTCHATaccountView.backgroundColor = [UIColor whiteColor];
        _HOTCHATaccountView.layer.masksToBounds = YES;
        _HOTCHATaccountView.layer.cornerRadius = 4;
    }
    return _HOTCHATaccountView;
}
- (UIImageView *)HOTCHATaccountlogoImgView{
    
    if (_HOTCHATaccountlogoImgView == nil) {
        
        _HOTCHATaccountlogoImgView = [[UIImageView alloc]init];
        _HOTCHATaccountlogoImgView.image = [UIImage imageNamed:@"l_login_ic_user"];
    }
    return _HOTCHATaccountlogoImgView;
}
- (UITextField *)HOTCHATaccounttextField{
    
    if (_HOTCHATaccounttextField == nil) {
        _HOTCHATaccounttextField = [[UITextField alloc] init];
        _HOTCHATaccounttextField.placeholder = NSLocalizedString(@"StrN011", nil);
        _HOTCHATaccounttextField.keyboardType = UIKeyboardTypeNumberPad;
        _HOTCHATaccounttextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _HOTCHATaccounttextField.textColor = [UIColor blackColor];
        _HOTCHATaccounttextField.font = [UIFont systemFontOfSize:15];
        
        [_HOTCHATaccounttextField setValue:HOTCHATHexColor(0xABA5B3) forKeyPath:@"_placeholderLabel.textColor"];
        NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
        NSString *accountNumber = [users objectForKey:@"lastAccountNumber"];
        // 记忆登陆
        if (accountNumber != nil && [accountNumber length] > 0) {
            
            _HOTCHATaccounttextField.text = [NSString stringWithFormat:@"%@",accountNumber];
        }
    }
    return _HOTCHATaccounttextField;
}
- (UIView *)HOTCHATpassWordView{
    
    if (_HOTCHATpassWordView == nil) {
        
        _HOTCHATpassWordView = [[UIView alloc]init];
        _HOTCHATpassWordView.backgroundColor = [UIColor whiteColor];
        _HOTCHATpassWordView.layer.masksToBounds = YES;
        _HOTCHATpassWordView.layer.cornerRadius = 4;
    }
    return _HOTCHATpassWordView;
}
- (UIImageView *)HOTCHATpassWordlogoImgView{
    
    if (_HOTCHATpassWordlogoImgView == nil) {
        
        _HOTCHATpassWordlogoImgView = [[UIImageView alloc]init];
        _HOTCHATpassWordlogoImgView.image = [UIImage imageNamed:@"l_login_ic_password"];
    }
    return _HOTCHATpassWordlogoImgView;
}
- (UITextField *)HOTCHATpassWordtextField{
    
    if (_HOTCHATpassWordtextField == nil) {
        _HOTCHATpassWordtextField = [[UITextField alloc] init];
        _HOTCHATpassWordtextField.placeholder = NSLocalizedString(@"StrN012", nil);
        _HOTCHATpassWordtextField.keyboardType = UIKeyboardTypeNumberPad;
        _HOTCHATpassWordtextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _HOTCHATpassWordtextField.textColor = [UIColor blackColor];
        _HOTCHATpassWordtextField.font = [UIFont systemFontOfSize:15];
        [_HOTCHATpassWordtextField setValue:HOTCHATHexColor(0xABA5B3) forKeyPath:@"_placeholderLabel.textColor"];
    }
    return _HOTCHATpassWordtextField;
}
- (UIButton *)HOTCHATloginBtn{
    
    if (_HOTCHATloginBtn == nil) {
        
        _HOTCHATloginBtn = [[UIButton alloc]init];
        [_HOTCHATloginBtn setBackgroundImage:[[UIImage imageNamed:@"l_btn_login"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 20, 1, 20) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateNormal];
        [_HOTCHATloginBtn setBackgroundImage:[[UIImage imageNamed:@"l_btn_login_hig"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 20, 1, 20) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateHighlighted];
        [_HOTCHATloginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_HOTCHATloginBtn setTitle:NSLocalizedString(@"StrN05", nil) forState:UIControlStateNormal];
        [_HOTCHATloginBtn addTarget:self action:@selector(HOTCHATloginAction) forControlEvents:UIControlEventTouchUpInside];
        _HOTCHATloginBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    }
    return _HOTCHATloginBtn;
}
//backaction
- (void)HOTCHATbackAction{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma login action
- (void)HOTCHATloginAction{
    
    [self.view endEditing:YES];
    [self HOTCHATstartLoading];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:self.HOTCHATaccounttextField.text forKey:@"accountNumber"];
    [dic setValue:self.HOTCHATpassWordtextField.text forKey:@"password"];
    [NetWorkTool HOTCHATRequestWithStringURL:LOGIN_URL progress:^(NSProgress * _Nonnull progress) {
        
    } RequestWay:@"GET" Parma:dic SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
        [self HOTCHATstopLoading];
        if ([resultDictionary[@"code"] integerValue] == 0) {
            
            UserInfoModel *model = [[UserInfoModel alloc]init];
            [model setValuesForKeysWithDictionary:resultDictionary[@"data"]];
            [model HOTCHATsaveTheUserInfo];
            
            AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [appdelegate initTabbarMenu];
        }
        
    } FailureBlock:^(NSError * _Nonnull error) {
        [self HOTCHATstopLoading];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
